<?php
/**
 * 
 * @todo description of this class
 * 
 * @author Alton Crossley <crossleyframework@nogahidebootstrap.com>
 * @package Crossley Framework
 *  
 * @copyright Copyright (c) 2003-2009, Nogaide BootStrap INC. All rights reserved.
 * @license BSD http://opensource.org/licenses/bsd-license.php
 * @version $Id:$
 * 
 */

/**
 * @namespace
 */
namespace X\DataStructure;

class ArrayUtil
{
	/**
	 * Remove the specified element from an array
	 * return the value
     *
     * @param array $aArray
     * @param mixed $sKey
     * @return mixed
     */
	public static function popKey(Array &$Array, $Key)
	{
		if (array_key_exists($Key, $Array))
		{
			$Return = $Array[$Key];
			unset($Array[$Key]);
			return $Return;
		}

		return false;
	}
	
	/**
     * make an array into human readable PHP to aid code generation
     * Note: not the same as serializing
     * 	Also does not handle complex objects
     * 
     * @param $a
     */
    static public function toSource(array $Array, $sIndent = null)
    {
    	// make sure we dont get in an infinate loop
    	static $Depth = 0;
    	$Depth++;
    	if ($Depth > 5) return 'value beyond depth restrictions';
    	
    	$EOL = "\n";
    	
    	$Return = 'array(' . $EOL;
    	$Values = null;
    	foreach ($Array as $Key => $Value)
    	{
    		if (strlen($Values)) $Values .=  $EOL . ',';
    		if (is_object($Value))
    		{
    			// @todo make an interface for this
    			if (method_exists($Value, 'toSource'))
    			{
    				$Value = $Value->toSource();
    			}
    			else
    			{
    				$Value = (array)$Value;
    			}
    		} 
    		
    		if (is_array($Value))
    		{
    			$Values .= '"' . $Key . '"' . ' => ' . self::toSource($Value) ;
    		}
    		else if (is_integer($Value))
    		{
    			$Values .= '"' . $Key . '"' . ' => ' . $Value;
    		}
    		else 
    		{
    			$Values .= '"' . $Key . '"' . ' => "' . (string)$Value . '"';
    		}
    		
    	}
    	$Return .= $Values.')';
    	
    	$Depth--;
    	
    	return $Return;
    }	
}